$(function(){
    var id=location.search.split('=')[1];
    console.log(id);
    
    //获取MP3
    $.ajax({
        type:'get',
        url:'/song/url',
        data:{id:id},
        success:function(e){
            // console.log(e);
            $("audio").prop("src",e.data[0].url)
            setTimeout(function(){
                $(".move1").click()
                console.log('播放了');
            },300)
        }
    })
    $(".move1,.open").on("click",function(){
        if($("audio")[0].paused){
            $("audio")[0].play()
            $(".open").hide()
            $(".move").removeClass("stopMove")
            console.log($("audio")[0]);
        } else {
            $(".move").addClass("stopMove")
            $(".open").show()
            $("audio")[0].pause()
            
        }
    })
    // 获取歌曲详情信息
    $.ajax({
        type:'get',
        url:'/song/detail',
        data:{ids:id},
        success:function(e){
            // console.log(e);
            var div=$(`<div class="filter_body" style="float: left;background: url(${e.songs[0].al.picUrl}) -230px -87px;"></div>`)
            $("body").prepend(div)
            var html=`<img src="images/music_ring.png" class="music_ring" >
            <img src="${e.songs[0].al.picUrl}" class="move" >
            `
            var html2=`<div class="music_title">
            <span>${e.songs[0].al.name}</span>- <span>${e.songs[0].ar[0].name}</span>
            </div>`
            $(".music_box").html(html)
            $(".box_center").prepend(html2)
        }
    })
    // 获取歌词
    $.ajax({
        type:'get',
        url:'/lyric',
        data:{id:id},
        success:function(e){
            console.log(e);
            var arr=e.lrc.lyric.split("\n");
            // console.log(arr);
            // console.log(1);
            arr1=[]
            $.each(arr,function(index,value){
                if(index<arr.length-1){
                    arr1.push(value.split("[")[1])
                }
            })
            // console.log(arr1);
            var timer=[]
            var musicword=[]
            $.each(arr1,function(index,value){
                timer.push(value.split("]")[0])
                musicword.push(value.split("]")[1])
                // console.log(value);
            })
            // console.log(timer);
            // console.log(musicword);
            var newtime=[];
            $.each(timer,function(index,value){
                var a=value.split(":")[0];
                var b=value.split(":")[1];
                newtime.push(parseInt(a*60+b*1))
            })
            // console.log(newtime);
            var obj={}
            for(let i=0;i<newtime.length;i++){
                obj[newtime[i]]=musicword[i]
            }
            // console.log(obj);

            $.each(musicword,function(index,value){
                var li=document.createElement("li");
                li.innerHTML=value;
                li.id=newtime[index]
                $(".song_word ul").append(li)
            })


            var q=0;
            $("audio")[0].ontimeupdate=function(){
                var time=this.currentTime;
                var total=this.duration;
                // console.log(time,total);
                if(time==total){
                    $(".move").addClass("stopMove")
                    $(".open").show()
                    $("audio")[0].pause()
                }
                for(var k in obj){
                    if(k==parseInt(time)){
                        i=$('*[id="'+k+'"').index();
                        $(".song_word ul li").css("color",'#fff')
                        $(".song_word ul li").eq(i).css('color','pink')
                        break;
                    }
                }
                $(".song_word ul").css('transform','translateY('+-(i+1)*30+'px');
            }
            
        }
    })
   

})